home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3649 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.3 KB  |  90 lines

  1. Path: news.reed.edu!usenet
  2. From: Peter Folk <pfolk@uni.uiuc.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: How to: friend template function
  5. Date: Wed, 24 Jan 1996 18:18:34 -0500
  6. Organization: Reed College,  Portland, Oregon
  7. Message-ID: <3106BE4A.2DF8@uni.uiuc.edu>
  8. NNTP-Posting-Host: 134.10.17.160
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b6a (Macintosh; I; PPC)
  13.  
  14. Hey,
  15.  
  16. While working on my most current project, I needed to implement some
  17. socket communication.  So, I took my normal C socket routines, got
  18. my file descriptor, and attach()ed it to a Server class that inherited 
  19. from fstream.  Unfortunately, for some reason this was disasterous...
  20. hanging up the connection at odd times, not recieving the input the
  21. server sent, etc, etc, so after a lot of fooling, I figured out that
  22. it worked if I had separate in and out streams.  Thanks to my attempts
  23. at OOP (ie a hidden implementation), I expeced this to be an easy
  24. change...  all I had to do was redefine Server:
  25.  
  26. class Server : public fstream {
  27.     fstream io;
  28.        // ...
  29. }
  30.  
  31.     to
  32.  
  33. class Server {
  34.     ifstream in;
  35.     ofstream out;
  36.       // ...
  37.   public:
  38.     Server &operator<<(/* each type I need */);
  39.     Server &operator>>(/* each type I need */);
  40. }
  41.  
  42.  
  43. And then it occurred to me that I should just make the >> and <<
  44. operators templates:
  45.  
  46. template <class T>
  47.     Server &operator<<(Server &s, T &t)
  48. {
  49.     s.out<<t; return *this;
  50. }
  51.  
  52. template <class T>
  53.     Server &operator>>(Server &s, T &t)
  54. {
  55.     s.in>>t; return *this;
  56. }
  57.  
  58.  
  59.  
  60. But, whoops!  I needed to give >> and << access to in and out
  61. so Server turns into:
  62.  
  63. class Server {
  64.   friend template <class T> Server &operator<<(Server &, T &);
  65.   friend template <class T> Server &operator>>(Server &, T &);
  66.  
  67.     ifstream in;
  68.     ofstream out;
  69.       // ...
  70. }
  71.  
  72.  
  73.  
  74. Now, my question is, why does my compiler complain so violently
  75. about that, and is there a way to do this without declaring in
  76. and out public?
  77.  
  78.  
  79. Thanks for any help,
  80.     Peter Folk
  81.  
  82.  
  83. p.s. If anyone knows why the fstream inheritance wasn't working, I'd
  84. love to hear about that, too.
  85.  
  86. p.p.s. If there's something fundamentally bad about what I'm doing,
  87. that would be nice to know, too... I've been programming C++ for,
  88. oh, at least a month now, so I really expect my code to be perfect
  89. (sarcasm).
  90.